/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.cef.designtime.api.changecontrolrule.controlruledef.basic.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.inspur.edp.cef.designtime.api.changecontrolrule.controlruledef.basic.entity.ControlRuleDefItem;
import com.inspur.edp.cef.designtime.api.changecontrolrule.controlruledef.basic.entity.ControlRuleDefinition;
import com.inspur.edp.cef.designtime.api.json.SerializerUtils;
import java.util.Map;

/**
 * The Json Serializer Of ControlRuleDef
 *
 * @ClassName: ControlRuleDefSerializer
 * @Author: Benjamin Gong
 * @Date: 2021/1/11 17:13
 * @Version: V1.0
 */
public class ControlRuleDefSerializer<T extends ControlRuleDefinition> extends JsonSerializer<T> {
    @Override
    public void serialize(T controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        SerializerUtils.writeStartObject(jsonGenerator);
        writeSelfInfos(controlRuleDefinition,jsonGenerator,serializerProvider);
        writeExtendInfos(controlRuleDefinition,jsonGenerator,serializerProvider);
        SerializerUtils.writeEndObject(jsonGenerator);
    }

    private void writeSelfInfos(ControlRuleDefinition controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        writeRuleObjectType(controlRuleDefinition,jsonGenerator,serializerProvider);
        writeSelfControlRules(controlRuleDefinition,jsonGenerator,serializerProvider);
        writeChildControlRules(controlRuleDefinition,jsonGenerator,serializerProvider);
    }

    private void writeRuleObjectType(ControlRuleDefinition controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        SerializerUtils.writePropertyValue(jsonGenerator, ControlRuleDefNames.RuleObjectType,controlRuleDefinition.getRuleObjectType());
    }

    private void writeSelfControlRules(ControlRuleDefinition controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        SerializerUtils.writePropertyName(jsonGenerator, ControlRuleDefNames.SelfControlRules);
        SerializerUtils.WriteStartArray(jsonGenerator);
        innerWriteSelfControlRules(controlRuleDefinition,jsonGenerator,serializerProvider);
        SerializerUtils.WriteEndArray(jsonGenerator);
    }

    private void innerWriteSelfControlRules(ControlRuleDefinition controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        if(controlRuleDefinition.getSelfControlRules()==null||controlRuleDefinition.getSelfControlRules().size()==0)
            return;
        for(Map.Entry<String, ControlRuleDefItem> item:controlRuleDefinition.getSelfControlRules().entrySet())
        {
           SerializerUtils.writePropertyValue_Object(jsonGenerator,item.getValue());
        }
    }

    private void writeChildControlRules(ControlRuleDefinition controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        SerializerUtils.writePropertyName(jsonGenerator, ControlRuleDefNames.ChildControlRules);
        SerializerUtils.writeStartObject(jsonGenerator);
        innerWriteChildRules(controlRuleDefinition,jsonGenerator,serializerProvider);
        SerializerUtils.writeEndObject(jsonGenerator);
    }

    private void innerWriteChildRules(ControlRuleDefinition controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
        if(controlRuleDefinition.getChildControlRules()==null||controlRuleDefinition.getChildControlRules().size()==0)
            return;
        for(Map.Entry<String,ControlRuleDefinition> item:controlRuleDefinition.getChildControlRules().entrySet())
        {
            SerializerUtils.writePropertyName(jsonGenerator,item.getKey());
//            SerializerUtils.writeStartObject(jsonGenerator);
            SerializerUtils.writePropertyValue_Object(jsonGenerator,item.getValue());
//            SerializerUtils.writeEndObject(jsonGenerator);
        }
    }

    protected void writeExtendInfos(T controlRuleDefinition, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) {
    }

}
